board = {
  deselect: function(e){
    if(!$(this).hasClass('can_move')){
      board.current_move = '';
      $('.tile').removeClass('can_move').removeClass('selected');
    }
    
  },
  mouseenter: function(e){
    $(this).addClass('selected');
  },
  mouseleave: function(e){
      var move = e.data.move;
      if(move!=board.current_move){
        $(this).removeClass('selected');   
      } 
  },
  click: function(e){      
      var move = e.data.move;
      board.current_move = move;
      $('.tile').removeClass('can_move').removeClass('selected');
      $(this).addClass('selected');
      for(m in moves[move]){
        $('#tile-'+moves[move][m]).addClass('can_move');                 
      
    }
  },
  send_move: function(move){
      document.getElementById('id_your_move').value = move;
      $('.tile').unbind('click', false );
      $('.tile').removeClass('can_move').removeClass('selected');
      $('#board').addClass('busy');
      $.post($("#move_form")[0].action, $("#move_form").serialize(), board.move_done, "json");
  },
  promotion_piece_click: function(e){
    var piece = e.data.piece;
    board.send_move(promotion_dialog.move+'='+piece.toUpperCase());
    promotion_dialog.dialog("close");    
  },
  do_move: function(e){
    var move = board.current_move+'-'+this.id.substr(5,2);
    if(moves['promotions'][move]){
      promotion_dialog.move = move;
      promotion_dialog.empty();
      for(piece in moves['promotions'][move])
        promotion_dialog.append($("<img src='"+media_url+"szachy/tiles/"+moves['promotions'][move][piece]+"l.png'/>").click(
          {piece: moves['promotions'][move][piece]},
          board.promotion_piece_click
        ));
      promotion_dialog.dialog("open");
      
    } else {
       board.send_move(move);      
    }
        
  },

  move_done: function(data){
    $('#board').removeClass('busy');
    if(data['errors']){
      $.jnotify(data.errors, "error")
    } else {
      $("#board").empty().append(data.chessboard);
      $("#movelist").empty().append(data.moves_list.replace(/\n/g, "<br/>"));
      moves = [];
      board.check_my_turn();
    }
  },
  is_my_move_prepare: function() {        
    $('.tile').click(board.deselect);
    for(m in moves){
      $('#tile-'+m).unbind('click', false ).mouseenter(board.mouseenter).mouseleave({move: m}, board.mouseleave).click({move: m}, board.click);    
    }
  },
  check_my_turn: function(){
    $.getJSON( my_move_ajax_url, board.check_my_turn_handler);
  },
  check_my_turn_handler: function(data){    
    if(data.my_move){         
      $("#board").empty().append(data['chessboard']);
      $("#movelist").empty().append(data.moves_list.replace(/\n/g, "<br/>"));
      $.jnotify("Your move");
      moves = data['moves'];
      board.is_my_move_prepare();
    } else if(!data.ongoing) {
      location.href=data.redirect;
    } else {      
      setTimeout("board.check_my_turn()", 5000);
    }
  } ,
  current_move: ''
}

$(document).ready(function(){
  $('div.board').delegate('div.can_move', 'click', board.do_move).delegate('div.can_move', 'mouseenter', board.mouseenter).delegate('div.can_move', 'mouseleave', {move: ''}, board.mouseleave);    
  if(!my_move){
    board.check_my_turn();   
  } else {
    board.is_my_move_prepare();
  }
  promotion_dialog = $("<div></div>").dialog({ autoOpen: false, modal: true, title: 'Promote' });

})

